// pages/login/index.js
import {Token} from '../../models/token.js'
import {MiniUserModel} from '../../models/miniuser.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    isHide: false
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    const token = new Token()
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userInfo']) {
          token.verify()
          wx.switchTab({
            url: '/pages/shift/shift'
          })
        } else {
          that.setData({
            isHide: true
          })
        }
      },
      fail: ()=>{},
      complete: ()=>{}
    })
  },

  bindGetUserInfo: function (e) {
    if (e.detail.userInfo) {
      let that = this
      console.log('用户的信息如下：')
      console.log(e.detail.userInfo)
      let userInfo = e.detail.userInfo
      wx.setStorage({
        key: 'nickName',
        data: userInfo.nickName,
        success: (res)=>{
          wx.getStorage({
            key: 'nickName',
            success: (res)=>{
              let nickName = res.data
              let mcode = this.methods._createUserID()
              const token = new Token(nickName,mcode)
              token.getTokenFromServer()
              wx.setStorageSync('uid', mcode) 
            },
            fail: ()=>{},
            complete: ()=>{}
          });
        },
        fail: ()=>{},
        complete: ()=>{}
      })
      that.setData({
        isHide:false
      })
      wx.switchTab({
        url: '/pages/shift/shift'        
      })
    } else {
      wx.showModal({
        title: '警告',
        content: '您点击了拒绝授权，将无法进入小程序，请授权之后进入',
        showCancel: false,
        confirmText: '返回授权',
        success: (res) => {
          if(result.confirm){
            console.log('用户点击了“返回授权”')
          }
        }
      })
    }
  },

  methods : {
    _createUserID () {
      let mcode = new Date()
      let scode = String(mcode.getFullYear())
      scode += String(mcode.getMonth() + 1)
      scode += String(mcode.getDate())
      scode += String(mcode.getHours())
      scode += String(mcode.getMinutes())
      scode += String(mcode.getSeconds())
      scode += String(mcode.getMilliseconds())
      return scode
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})